package com.yjhb.android.sbz.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.entity.MyTask;
import com.yjhb.android.sbz.enums.TaskStatus;
import com.yjhb.android.sbz.manager.ResManager;
import com.jojo.android.zxlib.base.BaseRecyclerAdapter;
import com.jojo.android.zxlib.tool.ZxTimeTool;
import com.jojo.android.zxlib.view.CircleImageView;

import java.math.BigDecimal;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 创建者 zx
 * 创建时间 2020/12/17 10:56
 */
public class MyTaskListAdapter extends BaseRecyclerAdapter<MyTaskListAdapter.MyTaskListHolder, MyTask> {
    private final String TAG = "MyTaskListAdapter";

    public MyTaskListAdapter(Context context) {
        super(context);
    }

    @Override
    protected MyTaskListHolder getHolder(View v) {
        return new MyTaskListHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyTaskListHolder myTaskListHolder, int i) {
        final int index = i;
        final MyTask entity = data.get(index);

        Glide.with(context).load(entity.getAvatar()).apply(new RequestOptions().placeholder(R.mipmap.test_head)).into(myTaskListHolder.headIv);
        myTaskListHolder.userTv.setText(entity.getName());
        BigDecimal confirmMoney = entity.getConfirmMoney();
        myTaskListHolder.moneyTv.setVisibility(View.VISIBLE);
        if (confirmMoney == null || confirmMoney.floatValue() == 0f) {
            if (entity.getMoney() == null) {
                myTaskListHolder.moneyTv.setVisibility(View.GONE);
            }
            myTaskListHolder.moneyTv.setText("+" + entity.getMoney() + "元");
        } else {
            myTaskListHolder.moneyTv.setText("+" + confirmMoney + "元");
        }
        myTaskListHolder.lookDetail_tv.setVisibility(View.GONE);
        switch (TaskStatus.getStatus(entity.getState())) {
            //待审核
            case TASK_STATUS_SUBMIT:

                myTaskListHolder.dateTv.setText("提交时间:" + ZxTimeTool.getTimeStr(entity.getSubmitDateTime()));
                int fast = entity.getAuditFast();
                int date = entity.getAuditTime();
                String time = "" + date + "小时内审核完成";
                if (date > 24) {
                    time = (date / 24) + "天内审核完成";
                }
                if (fast == 1) {
                    time = "1个小时内审核完成";
                }
                myTaskListHolder.tipsTv.setTextColor(ResManager.getThemeColor());
                myTaskListHolder.tipsTv.setText("审核中(" + time + ")");
                myTaskListHolder.btn1.setText("修改任务");
                myTaskListHolder.btn1.setVisibility(View.VISIBLE);
                myTaskListHolder.btn2.setVisibility(View.GONE);
                break;
            //已通过
            case TASK_STATUS_PASS:
                myTaskListHolder.dateTv.setText("审核时间:" + ZxTimeTool.getTimeStr(entity.getAuditDateTime()));
                myTaskListHolder.tipsTv.setTextColor(ResManager.getThemeColor());
                myTaskListHolder.tipsTv.setText("您的任务审核已通过，奖励已发放");
                myTaskListHolder.btn1.setText("评价");
                if (entity.getCommentState() == 0) {
                    myTaskListHolder.btn1.setVisibility(View.VISIBLE);
                } else {
                    myTaskListHolder.btn1.setVisibility(View.GONE);
                }
                myTaskListHolder.btn2.setVisibility(View.GONE);
                break;
            //未通过
            case TASK_STATUS_FAIL:
                myTaskListHolder.dateTv.setText("审核时间:" + ZxTimeTool.getTimeStr(entity.getAuditDateTime()));
                myTaskListHolder.tipsTv.setTextColor(context.getResources().getColor(R.color.red_00));
                myTaskListHolder.tipsTv.setText(entity.getRejectReason());
                Date checkTime = entity.getAuditDateTime();
                //审核时间是否超过24小时
                boolean b = System.currentTimeMillis() - checkTime.getTime() > 1000 * 60 * 60 * 24;
                //重新提交的次数如果超过1次，就不能重新提交了，最多只有两次重新提交的机会
                //如果审核时间距离当前时间已经超过了24小时，那么也不能重新提交了。
                if (entity.getSubmitCounter() > 1 || b) {
                    myTaskListHolder.btn1.setVisibility(View.GONE);
                } else {
                    myTaskListHolder.btn1.setVisibility(View.VISIBLE);
                }
                //初始化按钮值
                myTaskListHolder.btn2.setText("举报");
                myTaskListHolder.btn2.setVisibility(View.VISIBLE);
                myTaskListHolder.btn1.setEnabled(true);
                myTaskListHolder.btn1.setText("重新提交");
                myTaskListHolder.btn1.setBackgroundResource(R.drawable.theme_btn);
                myTaskListHolder.btn1.setTextColor(Color.WHITE);

                //不存在争执，并且审核的时间不超过24小时，就显示举报按钮，不然隐藏
                if (entity.getDispute() == 0) {
                    if (b) {
                        myTaskListHolder.btn2.setVisibility(View.GONE);
                    } else {
                        myTaskListHolder.btn2.setVisibility(View.VISIBLE);
                    }
                }
                //争执被取消，不显示举报按钮
                else if (entity.getDispute() == 2) {
                    myTaskListHolder.btn2.setVisibility(View.GONE);
                }
                //如果存在争执，隐藏举报按钮，并且使重新提交按钮不可用。
                else {
                    myTaskListHolder.btn2.setVisibility(View.GONE);
                    myTaskListHolder.btn1.setVisibility(View.VISIBLE);
                    myTaskListHolder.btn1.setEnabled(false);
                    myTaskListHolder.btn1.setBackgroundColor(0);
                    myTaskListHolder.btn1.setTextColor(context.getResources().getColor(R.color.gray_dc));
                    myTaskListHolder.btn1.setText("已举报，该任务不可操作");
                }
                myTaskListHolder.lookDetail_tv.setVisibility(View.VISIBLE);
                myTaskListHolder.lookDetail_tv.setOnClickListener(v -> {
                    if (itemClickListener != null) {
                        itemClickListener.onViewClick(myTaskListHolder.lookDetail_tv, "lookDetail", index, entity);
                    }
                });
                break;

            //进行中
            case TASK_STATUS_TAKE:
                myTaskListHolder.dateTv.setText("报名时间:" + ZxTimeTool.getTimeStr(entity.getFetchDateTime()));
                myTaskListHolder.tipsTv.setText("任务进行中");
                myTaskListHolder.tipsTv.setTextColor(ResManager.getThemeColor());
                myTaskListHolder.btn2.setVisibility(View.GONE);
                myTaskListHolder.btn1.setText("取消报名");
                myTaskListHolder.btn2.setVisibility(View.VISIBLE);
                myTaskListHolder.btn2.setText("提交任务");
                break;
            //超时
            case TASK_STATUS_TIMEOUT:
                myTaskListHolder.dateTv.setText("报名时间:" + ZxTimeTool.getTimeStr(entity.getFetchDateTime()));
                myTaskListHolder.tipsTv.setText("任务已超时");
                myTaskListHolder.tipsTv.setTextColor(ResManager.getThemeColor());
                myTaskListHolder.btn2.setVisibility(View.VISIBLE);
                myTaskListHolder.btn2.setText("删除记录");
                myTaskListHolder.btn1.setText("重新报名");
                break;
        }


        myTaskListHolder.btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClickListener != null) {
                    itemClickListener.onViewClick(myTaskListHolder.btn1, "btn1", index, entity);
                }
            }
        });
        myTaskListHolder.btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClickListener != null) {
                    itemClickListener.onViewClick(myTaskListHolder.btn2, "btn2", index, entity);
                }
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.item_mytask;
    }


    class MyTaskListHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.head_iv)
        CircleImageView headIv;
        @BindView(R.id.user_tv)
        TextView userTv;
        @BindView(R.id.date_tv)
        TextView dateTv;
        @BindView(R.id.tips_tv)
        TextView tipsTv;
        @BindView(R.id.btn_1)
        TextView btn1;
        @BindView(R.id.btn_2)
        TextView btn2;
        @BindView(R.id.money_tv)
        TextView moneyTv;
        @BindView(R.id.lookDetail_tv)
        TextView lookDetail_tv;
        @BindView(R.id.bg_layout)
        ConstraintLayout bgLayout;
//        @BindView(R.id.line)
//        View line;

        public MyTaskListHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
